<?php

class Core_Model_Design_Collection_Block extends Dcmp_Model_Collection_Strong {

    public function __construct() {
        parent::__construct();
        $this->setItemClass('design/block');
        $this->from('{t}design_block', 'a')->field('a.*');
    }

    public function joinPageTemplate(){
        return $this->leftJoin('{t}design_page_template', 'c', 'a.page_template_id', 'c.id')
                    ->field('c.name', 'page_name');
    }

    public function joinArea() {
        return $this->innerJoin('{t}global_area', 'b','a.area_id', 'b.id')
                    ->field('c.name', 'area_name');
    }

    public function joinModule() {
        return $this->innerJoin('{t}global_module', 'd','a.module_id', 'd.id')
                    ->field('d.title', 'module_name')
                    ->field('d.name', 'module_type')
                    ->field('d.domain', 'domain');
    }

    public function joinBlockTemplate(){
         return $this->innerJoin('{t}design_block_template', 'e', 'a.block_template_id', 'e.id')
                    ->field('e.name', 'block_template_name')
                    ->field('e.file_name', 'block_template_file')
                    ->field('e.version', 'block_template_version')
                    ->field('e.website_theme', 'website_theme');
    }

    public function filterBy($name, $arg) {
        switch ($name) {
            case 'id':
                return $this->addParameter('id', $arg)->eq('a.id', ':id:s');
            case 'areaid':
                return $this->addParameter('areaId', $arg)->eq('a.area_id', ':areaId:s');
            case 'pagetemplateid':
                return $this->addParameter('page_template_id', $arg)->eq('a.page_template_id', ':page_template_id:s');
            case 'position':
                return $this->addParameter('position', $arg)->eq('a.position', ':position:s');
        }
        return parent::filterBy($name, $arg);
    }

    public function sortBy($name, $dir){
        switch($name){
            case 'sorting':
                return $dir == 'desc'? $this->desc('a.sorting'): $this->asc('a.sorting');
            case 'id':
                return $dir == 'desc'? $this->desc('a.id'): $this->asc('a.id');
            case 'moduleid':
                return $dir == 'desc'? $this->desc('a.moduleid'): $this->asc('a.moduleid');
            case 'pagetemplate':
                return $dir == 'desc'? $this->desc('a.page_template_id'): $this->asc('a.page_template_id');
            case 'position':
                return $dir == 'desc'? $this->desc('a.position'): $this->asc('a.position');
            case 'enabled':
                return $dir == 'desc'? $this->desc('a.enabled'): $this->asc('a.enabled');    
        }
        return parent::sortBy($name, $dir);
    }
}

